/**
 * Created by oneminute on 7/17/14.
 */

var lz = require("../lib/utils/modules")();

exports.get = function (_req, _res, _opts, _callback) {
    var lon = _opts.lon;
    var lat = _opts.lat;
    var type = _opts.type || 'commodities';
    var count = _opts.count || lz.Config.webServer.page;
    var p = _opts.p * count;
    lz.Logger.debug("/search: p = %d, count = %d", p, count);

    lz.ArangoDB.searchByGeo({lon: lon, lat: lat},
        lz.Config.business.searchRadius,
        lz.Config.business.searchLimit, p, count, type,
        function (_err, _items) {
            if (_err) {
                lz.Logger.error("/search: 搜索时发生错误。");
                _callback(_err, null, null);
                return;
            }
            lz.Logger.debug("/search: 搜索完毕，返回结果: ", _items.length);
            _callback(_err, null, _items, _items < lz.Config.webServer.page);
        }
    );
};